// 创建calendar窗口方法
import { BrowserWindow, ipcMain } from "electron";

export function openCalendarWindow() {
	const winURL = process.env.NODE_ENV === 'development'
		? process.env.WEBPACK_DEV_SERVER_URL
		: `app://./index.html`

	// 考虑先加载,点击之后触发show
	const newWin = new BrowserWindow({
		width: 900,
		height: 620,
		minWidth: 900,
		minHeight: 620,
		frame: true, //是否显示边缘框
		fullscreen: false, //是否全屏显示
		title: "项目名",
		show: false,
		// modal: true,
		// autoHideMenuBar: true
		webPreferences: {
			nodeIntegration: true, // 允许渲染进程使用node
			contextIsolation: false
		},
		// closable: false // 禁止关闭
	});

	newWin.loadURL(winURL + '#/test_hello')

	// newWin.once('ready-to-show', () => {
	// 	newWin.show()
	// })

	newWin.on('page-title-updated', (event) => {
		event.preventDefault() // 阻止修改title
		// console.group('page-title-updated');
		// console.log(event)
		// console.groupEnd();
	})

	newWin.on('close', function(event) {
		event.preventDefault() // 阻止默认关闭
		newWin.hide() // 改为hide
		console.log('close')
	});

	newWin.on('closed', function() {
		console.log('closed')
	});

	newWin.webContents.openDevTools()

	// 预加载之后绑定open事件
	ipcMain.on('open', e =>
		newWin.show()
	);

	// 获取当前窗口id
	const winId = newWin.id;
	console.log('winId', winId)

	const win = BrowserWindow.fromId(winId);
	// console.log('通过 id 查找窗口', win)
	if (win) {
		win.webContents.on('did-finish-load', (event: any) => {
			console.log('did-finish-load')
			win.webContents.send('msg', winId, '我是 index 的数据');
		})

		win.webContents.on('devtools-closed', (event: any) => {
			console.log('devtools-closed')
		})
	}

}
